package com.xja.springboot.controller;

import com.xja.springboot.entity.Chatuser;
import com.xja.springboot.service.ChatuserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("chatuser")
@SessionAttributes({"loginuser"})
public class ChatuserController {

    //日志
    private Logger logger = LoggerFactory.getLogger(ChatuserController.class);

    @Autowired
    private ChatuserService chatuserService;

    @RequestMapping("reg")
    public String reg(Chatuser chatuser){
        try {
            logger.info("注册注册");
            chatuserService.addChatUser(chatuser);
            return "redirect:/login.html";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/error.html";
    }

    @RequestMapping("login")
    public String login(Chatuser chatuser, Model model){
        try {
            logger.info("登录登录");
            Chatuser loginuser = chatuserService.selectByUserId(chatuser.getUserid());
            if (loginuser==null || !loginuser.getPassword().equals(chatuser.getPassword())){
                return "redirect:/login.html";
            }
            model.addAttribute("loginuser",loginuser);
            return "index";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/error.html";
    }

    @RequestMapping("touser")
    public String touser(Model model,Integer id){
        try {
            logger.info("查询个人信息");
            Chatuser chatuser = chatuserService.selectUserById(id);
            model.addAttribute("chatuser",chatuser);
            return "info-setting";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/error.html";
    }

    @RequestMapping("/{id}/update")
    public String update(@PathVariable("id") Integer id,Chatuser chatuser){
        try {
            logger.info("修改修改");
            chatuserService.updateUser(chatuser);
            return "redirect:/login.html";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/error.html";
    }

    @RequestMapping("upload")
    public String upload(Chatuser chatuser, MultipartFile img, HttpServletRequest request){
        try {
            logger.info("修改头像");
            String filename = img.getOriginalFilename();
            if (filename!=null && !"".equals(filename)){
                String typename =filename.substring(filename.lastIndexOf(".")+1);
                filename= UUID.randomUUID().toString()+"."+typename;
                String serverPath ="D:/idea/ssm_springboot_webchat/ssm_springboot_web/src/main/resources/static/images/"+filename;
                System.out.println("上传的路径:" + serverPath);
                img.transferTo(new File(serverPath));
            }
            chatuser.setProfilehead(filename);
            chatuserService.updateUser(chatuser);
            return "redirect:/login.html";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/error.html";
    }

    @RequestMapping("toxinxi")
    public String toxinxi(Model model,Integer id){
        try {
            logger.info("查询个人信息");
            Chatuser chatuser = chatuserService.selectUserById(id);
            model.addAttribute("chatuser",chatuser);
            return "information";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/error.html";
    }
}
